/***************************************************************
 * Name:      guiMain.cpp
 * Purpose:   Code for Application Frame
 * Author:    Retral ()
 * Created:   2007-06-09
 * Copyright: Retral (http://code.google.com/p/retral/)
 * License:
 **************************************************************/

#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "guiMain.h"
#include "waitingroom.h"
#include "connection.h"

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*InternalHeaders(guiFrame)
#include <wx/bitmap.h>
#include <wx/font.h>
#include <wx/fontenum.h>
#include <wx/fontmap.h>
#include <wx/image.h>
#include <wx/intl.h>
#include <wx/settings.h>
//*)

//(*IdInit(guiFrame)
const long guiFrame::ID_TEXTCTRL1 = wxNewId();
const long guiFrame::ID_BUTTON1 = wxNewId();
const long guiFrame::ID_BUTTON2 = wxNewId();
//*)

BEGIN_EVENT_TABLE(guiFrame,wxFrame)
	//(*EventTable(guiFrame)
	//*)
END_EVENT_TABLE()

guiFrame::guiFrame(wxWindow* parent,wxWindowID id)
{
	//(*Initialize(guiFrame)
	Create(parent,id,wxEmptyString,wxDefaultPosition,wxDefaultSize,wxDEFAULT_FRAME_STYLE,_T("wxFrame"));
	SetClientSize(wxSize(326,123));
	TextCtrl1 = new wxTextCtrl(this,ID_TEXTCTRL1,_("Username"),wxPoint(26,39),wxSize(177,23),0,wxDefaultValidator,_T("ID_TEXTCTRL1"));
	Button1 = new wxButton(this,ID_BUTTON1,_("Change Server"),wxPoint(75,83),wxDefaultSize,0,wxDefaultValidator,_T("ID_BUTTON1"));
	Button2 = new wxButton(this,ID_BUTTON2,_("Login"),wxPoint(227,35),wxDefaultSize,0,wxDefaultValidator,_T("ID_BUTTON2"));
	Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&guiFrame::OnLogin);
	//*)
}

guiFrame::~guiFrame()
{
	//(*Destroy(guiFrame)
	//*)
}

void guiFrame::OnLogin(wxCommandEvent& event)
{
    bool connected;
    Connection* serv = new Connection();
    connected = serv->init(TextCtrl1->GetValue().char_str(), DEFAULT_IP, DEFAULT_PORT);

    if(connected) {
        WaitingRoom* frame = new WaitingRoom(NULL, serv);
        serv->startReading();
        serv->addWaitingRoom(frame);
        frame->Show();

        Destroy();
    }else {
        // TODO: Failed to connect ask for new info.
    }
}
